#binConvertTester

from binConvert_Evan import * #to_bin1(n) to_bin2(n) to_bin3(n)
from binConvert_Bryan import * #binCon(n) binCon2(n)
from time import *
from random import randint,choice

def test_b(n):
    t0=clock()
    to_bin3(n)
    t1 = clock()
    return t1-t0

def test_e(n):
    t0=clock()
    binCon(n)
    t1 = clock()
    return t1-t0

def play_set(win_score,quan,lim):
    if win_score < 2:
        win_score = 2
        print('win_score changed to: 2')
    b_score = 0
    e_score = 0
    while decide_to_play(win_score,b_score,e_score):
        winner = play_game(quan,lim)
        if winner == 'b':
            b_score +=1
        else:
            e_score +=1
    print()
    if b_score > e_score:
        print("-Bryan wins set with score: " + str(b_score) + '-' + str(e_score))
        return 'b'
    print("-Evan wins set with score: " + str(e_score) + '-' + str(b_score))
    return 'e'

def decide_to_play(win_score,b_score,e_score):
    if b_score >= win_score:
        if b_score > e_score:
            if b_score - e_score >=2:
                return False
    if e_score >= win_score:
        if e_score > b_score:
            if e_score - b_score >=2:
                return False
    return True    

def choose_first(): #chooses who will go first for a given game
    return choice(['b','e'])

def play_game(quan, lim): #lowest level of competition
    if choose_first() == 'b':
        return game_bry_first(quan, lim)
    return game_evan_first(quan,lim)

def game_evan_first(quan, lim):#Same as game_bry_first() but evan moves first, has adv
    b_score = 0
    e_score = 0
    for i in range(quan):
        test_val = randint(1,lim)
        e_score += test_e(test_val)
        b_score += test_b(test_val)
    #print(b_score,e_score)
    if b_score < e_score:
        print("Bryan wins game by:",e_score-b_score, "-->", (e_score-b_score)/e_score*100, '%')
        return 'b'
    print("Evan wins game by: ",b_score-e_score, "-->", (b_score-e_score)/b_score*100,'%')
    return 'e'

def game_bry_first(quan, lim):#Same as game_evan_first() but bry moves first, has adv
    b_score = 0
    e_score = 0
    for i in range(quan):
        test_val = randint(1,lim)
        b_score += test_b(test_val)
        e_score += test_e(test_val)
    #print(b_score,e_score)
    if e_score < b_score:
        print("Evan wins game by: ",b_score-e_score, "-->", (b_score-e_score)/b_score*100,'%')
        return 'e'
    print("Bryan wins game by:",e_score-b_score, "-->", (e_score-b_score)/e_score*100, '%')
    return 'b'

def run_test():
    win_sets = 2
    win_game = 3
    quan = 100000
    lim  = 100000000
    
    if win_sets < 2:
        win_sets = 2
        print('win_sets changed to: 2')
    b_score = 0
    e_score = 0
    print("Match Start")
    while decide_to_play(win_sets,b_score,e_score):
        winner = play_set(win_game,quan,lim)
        if winner == 'b':
            b_score +=1
        else:
            e_score +=1
        print("*MatchScoreB-E :", str(b_score)+'-'+str(e_score))
        print()
    if b_score > e_score:
        print("*Bryan wins match with score: " + str(b_score) + '-' + str(e_score))
    else:
        print("*Evan wins match with score: " + str(e_score) + '-' + str(b_score))

def test_bin(func):
    test_list = [2091658,4474,237569,31457281,180143985094819,2287870,314159]
    for i in range(1,10000001):
        if str(eval(func)) != bin(i)[2:]:
            print("Failed on", i)
            return False,i
    for i in test_list:
        if str(eval(func)) != bin(i)[2:]:
            print(func, "Failed on", i)
            return False,i
    return True